<?php

namespace App\Http\Controllers\Backstage\Menu;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
//菜单管理
class MenuController extends Controller
{
    //菜单列表
    public function Menumanage(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = DB::table('menu')->get();
        if ($info) {
            $info = json_decode($info, true); //对象转成数组
            foreach ($info as $key => $value) {
                $info[$key]['create_time'] = date('Y-m-d', $value['create_time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //添加菜单
    public function addmenu(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $data['title'] = $request->input('title');
        $data['create_time'] = time();
        $info = DB::table('menu')->insert($data);
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }

    //修改菜单
    public function editmenu(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['menu_id'] = $request->input('menu_id');
        $data['title'] = $request->input('title');
        $info = DB::table('menu')->where($map)->update($data);
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }

    //删除菜单
    public function delmenu(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $data['menu_id'] = $request->input('menu_id');
        $info = DB::table('menu')->where($data)->delete();
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
}
